﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Engine.Managers.*;
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import flash.utils.*;
    import Classes.util.*;
    import Display.*;
    import Display.MarketUI.*;
    import Modules.zoo.events.*;
    import Modules.zoo.*;
    import com.adobe.utils.*;
    import com.adobe.crypto.*;
    import Modules.zoo.transactions.*;

    public class ZooEnclosure extends Municipal implements IMechanicPrePlayAction {

        private const ZOO:String = "zooEnclosure";
        private const FEED_UPDATE_INTERVAL:int = 300;

        private var m_zooFeedUpdateTime:Number = 0;

        public function ZooEnclosure(_arg1:String){
            super(_arg1);
            m_objectType = WorldObjectTypes.ZOO_ENCLOSURE;
            m_typeName = this.ZOO;
        }
        override public function onBuildingConstructionCompleted_PreServerUpdate():void{
            this.addRandomAnimal(ZooManager.COMMON);
            super.onBuildingConstructionCompleted_PreServerUpdate();
        }
        public function onPrePlayTrack():void{
            StatsManager.sample(100, "Game_action", "Zoo", "click_on_zoo_not_havest", this.getItemName());
        }
        public function getAnimals():int{
            var _local1:IStorageMechanic = (MechanicManager.getInstance().getMechanicInstance(this, ZooManager.MECHANIC_STORAGE, MechanicManager.ALL) as IStorageMechanic);
            var _local2:ISlotMechanic = (MechanicManager.getInstance().getMechanicInstance(this, ZooManager.MECHANIC_SLOTS, MechanicManager.ALL) as ISlotMechanic);
            if (((_local1) && (_local2))){
                return ((_local1.getTotalCount() + _local2.numFilledSlots));
            };
            return (0);
        }
        public function getNumSpecificAnimal(_arg1:String):int{
            var _local2:IStorageMechanic = (MechanicManager.getInstance().getMechanicInstance(this, ZooManager.MECHANIC_STORAGE, MechanicManager.ALL) as IStorageMechanic);
            var _local3:ISlotMechanic = (MechanicManager.getInstance().getMechanicInstance(this, ZooManager.MECHANIC_SLOTS, MechanicManager.ALL) as ISlotMechanic);
            return ((_local2.getCount(_arg1) + _local3.getNumItems(_arg1)));
        }
        public function addRandomAnimal(_arg1:String):void{
            var _local2:IStorageMechanic = (MechanicManager.getInstance().getMechanicInstance(this, ZooManager.MECHANIC_STORAGE, MechanicManager.ALL) as IStorageMechanic);
            var _local3:Dictionary = Global.zooManager.getAllAnimalsByRarity(m_item.name);
            var _local4:Array = _local3[_arg1];
            var _local5:String = ((Global.player.uid + "::") + getItemName());
            var _local6:String = ("0x" + MD5.hash(_local5).substring(0, 8));
            var _local7:Number = Number(_local6);
            var _local8:int = (_local7 % _local4.length);
            var _local9:String = _local4[_local8];
            if (_local2){
                _local2.sendTransactions = false;
                _local2.add(_local9, 1);
            };
        }
        public function hasAllAnimals():Boolean{
            var _local1:Array;
            var _local5:Item;
            var _local2:Dictionary = new Dictionary();
            var _local3:IStorageMechanic = (MechanicManager.getInstance().getMechanicInstance(this, ZooManager.MECHANIC_STORAGE, MechanicManager.ALL) as IStorageMechanic);
            var _local4:ISlotMechanic = (MechanicManager.getInstance().getMechanicInstance(this, ZooManager.MECHANIC_SLOTS, MechanicManager.ALL) as ISlotMechanic);
            if (((_local3) && (_local4))){
                _local1 = Global.gameSettings.getItemsByKeywords(_local3.restrictedKeywords);
                for each (_local5 in _local1) {
                    if ((((_local3.getCount(_local5.name) > 0)) && ((_local2[_local5.name] == null)))){
                        _local2[_local5.name] = true;
                    };
                    if ((((_local4.getNumItems(_local5.name) > 0)) && ((_local2[_local5.name] == null)))){
                        _local2[_local5.name] = true;
                    };
                };
            };
            return ((DictionaryUtil.getKeys(_local2).length >= _local1.length));
        }
        public function getPayout():int{
            var _local3:Item;
            var _local1:Array = Global.zooManager.getAllAnimals(this.getItemName());
            var _local2:int;
            for each (_local3 in _local1) {
                _local2 = (_local2 + _local3.inventoryLimit);
            };
            return (((this.getAnimals() * 100) / _local2));
        }
        public function unlockNextEnclosure(_arg1:String="readyToUnlock"):Boolean{
            var _local2:Boolean;
            var _local3:Boolean;
            switch (_arg1){
                case "readyToUnlock":
                    _local3 = this.readyToUnlock();
                    break;
                case "readyToUnlockMinusAnimals":
                    _local3 = this.readyToUnlockMinusAnimals();
                    break;
            };
            if (_local3){
                Global.player.setSeenSessionFlag((ZooManager.ZOO_UNLOCK + m_item.unlocksItem));
                Global.player.inventory.addSingletonItem(m_item.unlocksItem);
                GameTransactionManager.addTransaction(new TZooUnlock(m_item));
                Global.ui.dispatchEvent(new ZooDialogEvent(ZooDialogEvent.UNLOCK_ENCLOSURE, m_item.unlocksItem));
                _local2 = true;
            };
            return (_local2);
        }
        public function readyToUnlock():Boolean{
            return (((((m_item.unlocksItem) && (this.hasAllAnimals()))) && (BuyLogic.isLocked(Global.gameSettings.getItemByName(m_item.unlocksItem)))));
        }
        public function readyToUnlockMinusAnimals():Boolean{
            return (((m_item.unlocksItem) && (BuyLogic.isLocked(Global.gameSettings.getItemByName(m_item.unlocksItem)))));
        }
        override public function onUpdate(_arg1:Number):void{
            super.onUpdate(_arg1);
            this.m_zooFeedUpdateTime = (this.m_zooFeedUpdateTime + _arg1);
            if (this.m_zooFeedUpdateTime > this.FEED_UPDATE_INTERVAL){
                this.m_zooFeedUpdateTime = 0;
                if (Global.world.viralMgr.canPost(getItem().feed)){
                    updateStagePickEffect();
                };
            };
        }
        override public function sell():void{
            var _local1 = "";
            if (this.getAnimals() > 0){
                _local1 = ZLoc.t("Main", "SendToInventoryZooAnimalsWarning", {
                    item:getItemFriendlyName(),
                    coins:getSellPrice()
                });
                UI.displayMessage(_local1, GenericPopup.TYPE_YESNO, sellConfirmationHandler);
            } else {
                super.sell();
            };
        }

    }
}//package Classes 
